package com.genesis.yunnanji.view;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;

import com.genesis.yunnanji.R;
import com.youth.xframe.widget.XStateButton;

import java.util.LinkedHashMap;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2018/9/21 on 16:13
 * 作者:Kite
 * 描述:设置新密码
 */
public class SetNewPasswordActivity extends BaseActivity {

    @BindView(R.id.et_password)
    EditText etPassword;

    @BindView(R.id.et_confirm_password)
    EditText etConfirmPassword;

    @BindView(R.id.btn_next)
    XStateButton btnNext;

    private LinkedHashMap<Integer, String> errorMap = new LinkedHashMap<>();

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_new_password;
    }

    @Override
    public void initView() {
        super.initView();
        setTitle("设置新密码");
        editIsEmpty();
    }

    @OnClick(R.id.btn_next)
    public void onViewClicked() {
        advance(IsBindingSuccessActivity.class, 4);
    }

    private void editIsEmpty() {
        TextWatcher passwordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(5);
                        if (code.length() >= 8 && code.length() <= 16) {
                            errorMap.remove(6);
                        } else {
                            errorMap.put(6, "请输入正确的密码");
                        }
                    } else {
                        errorMap.put(5, "请输入密码");
                    }
                    changeClickable();
                }
            }
        };
        etPassword.addTextChangedListener(passwordWatcher);
        TextWatcher confirmPasswordWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable != null) {
                    String code = editable.toString();
                    if (!TextUtils.isEmpty(code)) {
                        errorMap.remove(7);
                        if (code.length() >= 8 && code.length() <= 16) {
                            errorMap.remove(8);
                        } else {
                            errorMap.put(8, "请输入正确的密码");
                        }
                    } else {
                        errorMap.put(7, "请输入密码");
                    }
                    changeClickable();
                }
            }
        };
        etConfirmPassword.addTextChangedListener(confirmPasswordWatcher);
        errorMap.put(5, "请输入新的登录密码");
        errorMap.put(7, "请确认新的登录密码");

        btnNext.setClickable(false);
        btnNext.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
    }

    public void changeClickable() {
        if (errorMap.isEmpty()) {
            btnNext.setClickable(true);
            btnNext.setNormalBackgroundColor(getResources().getColor(R.color.colorTheme));
        } else {
            btnNext.setClickable(false);
            btnNext.setNormalBackgroundColor(getResources().getColor(R.color.color_ccc));
        }
    }

}
